import AsyncStorage from "@react-native-async-storage/async-storage";

export default class Storage{
    static async getItem(key){
        try{
           const data = await AsyncStorage.getItem(key)
           return data ? JSON.parse(data) : data
        }catch(e){
            console.log(e)
        }
    }

    static async setItem(key, value){
        try{
            const data = JSON.stringify(value)
            await AsyncStorage.setItem(key, data ? data : '')
        }catch(e){
            console.log(e)
        }
    }

    static async getItems(keys){
        try {
            values = await AsyncStorage.multiGet(keys)
            let newArr = values.map(item => {
                const val = JSON.parse(item[1])
                return [item[0], val]
            })
            return newArr
          } catch(e) {
            // read error
            console.log(e)
          }
    }

    static async setItems(keyValArr){
        try {
            let newArr = keyValArr.map((item) => {
                const val = JSON.stringify(item[1])
                return [item[0], val ? val : '']
            })
            values = await AsyncStorage.multiSet(newArr)
          } catch(e) {
            // read error
            console.log(e)
          }
    }
}